package cn.darkhorse.device.service.impl;

import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.common.util.DateUtilOther;
import cn.darkhorse.common.util.MsgUtil;
import cn.darkhorse.device.mapper.MesMsgAuditMapper;
import cn.darkhorse.device.pojo.MesMsgAudit;
import cn.darkhorse.device.service.MesMsgAuditService;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 单据审核 服务实现类
 * </p>
 *
 * @author yuan jun
 * @since 2023-02-03
 */
@Service
public class MesMsgAuditServiceImpl extends ServiceImpl<MesMsgAuditMapper, MesMsgAudit> implements MesMsgAuditService {

    /**
     * 发送消息信息
     *
     * @param businessId
     * @param uuid
     */
    @Override
    public void sendAuditMsg(String typeName, Long businessId, String uuid) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", typeName);
        jsonObject.put("businessId",businessId);
        jsonObject.put("createTime", LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        jsonObject.put("uuid",uuid);
        MsgUtil.sendAuditMsg(jsonObject.toJSONString());
    }

    /**
     * 更新审核消息
     *
     * @param businessId
     * @param approver
     * @param approveTime
     */
    @Override
    public void upAuditMsg(Long businessId, Long approver, String approveTime,Integer auditStatus,Integer BusinessType) {
        MesMsgAudit audit=new MesMsgAudit();
        audit.setAuditFlag(CommonConstant.MSG_READ);
        audit.setAuditResult(CommonConstant.AUDIT_STATUS_REVIEWED.equals(auditStatus)?"审核通过":"审核不通过");
        audit.setAuditorId(approver);
        LocalDateTime localDateTime=DateUtilOther.parseLocalDatetime(approveTime);
        if(null==localDateTime){
            localDateTime=LocalDateTime.parse(approveTime);
        }
        audit.setAuditTime(localDateTime);
        LambdaQueryWrapper<MesMsgAudit> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesMsgAudit::getBusinessId,businessId);
        wrapper.eq(MesMsgAudit::getBusinessType, BusinessType);
        wrapper.eq(MesMsgAudit::getAuditFlag,CommonConstant.MSG_UNREAD);
        update(audit,wrapper);
    }

    /**
     * 删除未审核已删除单据
     *
     * @param businessId
     * @param BusinessType
     */
    @Override
    public void removeMsg(Long businessId, Integer BusinessType) {
        MesMsgAudit audit=new MesMsgAudit();
        audit.setIsDelete(CommonConstant.IS_DELETE_YES);
        LambdaQueryWrapper<MesMsgAudit>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesMsgAudit::getBusinessId,businessId);
        wrapper.eq(MesMsgAudit::getBusinessType,BusinessType);
        wrapper.eq(MesMsgAudit::getAuditFlag,CommonConstant.MSG_UNREAD);
        update(audit,wrapper);
    }

    /**
     * 列表
     *
     * @param userId
     * @param auditFlag
     * @return
     */
    @Override
    public List<MesMsgAudit> list(Long userId, Integer auditFlag) {
        if(null==userId){
            throw new ServiceException("缺失用户信息");
        }
        LambdaQueryWrapper<MesMsgAudit> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesMsgAudit::getReceiverId,userId);
        wrapper.eq(MesMsgAudit::getIsDelete,CommonConstant.IS_DELETE_NO);
        if(null!=auditFlag){
            wrapper.eq(MesMsgAudit::getAuditFlag,auditFlag);

            if(CommonConstant.MSG_READ.equals(auditFlag)){
               wrapper.eq(MesMsgAudit::getAuditorId,userId);
               wrapper.orderByDesc(MesMsgAudit::getAuditTime);
            }else{
                wrapper.orderByAsc(MesMsgAudit::getCreateTime);
            }
        }
        List<MesMsgAudit> list = list(wrapper);
        return list;
    }

}
